import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Welcome from '@/components/Welcome'
import users from '@/components/users/users'
import categories from '@/components/goods/categories'
import goods from '@/components/goods/goods'
import params from '@/components/goods/params'
import add from '@/components/goods/add'
import orders from '@/components/orders/orders'
import reports from '@/components/reports/reports'
import rights from '@/components/rights/rights'
import roles from '@/components/rights/roles'



import { Message } from 'element-ui'
Vue.use(VueRouter)
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home', component: Home, redirect: '/welcome', children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: users },
      { path: '/categories', component: categories },
      { path: '/goods', component: goods },
      { path: '/goods/add', component: add },
      { path: '/params', component: params },
      { path: '/orders', component: orders },
      { path: '/reports', component: reports },
      { path: '/rights', component: rights },
      { path: '/roles', component: roles }
    ]
  }
]
const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) {
    Message({
      showClose: true,
      message: '你还没有登录！请登录后再执行操作~~',
      type: 'error'
    })
    return next('/login')
  }
  next()
})
export default router
